package com.project.common.exception;

import org.apache.commons.lang3.StringUtils;

public class BusinessException extends RuntimeException {
    private ErrorCode errorCode;
    private String error;

    private static final String prefix = "操作异常，参考信息：";

    public BusinessException(String message) {
        super(prefix + message);
        if (StringUtils.isNotBlank(message)) {
            message = prefix + message;
        }
        this.error = message;
        this.errorCode = ErrorCode.of(ErrorCodes.Common.ERROR_UNKNOWN_ERROR.errorCode().getCode(), message);
    }

    public BusinessException(ErrorCode errorCode) {
        this(errorCode.getMessage(), errorCode);
    }

    public BusinessException(Throwable throwable) {
        this(prefix + throwable.getMessage(), throwable);
    }

    public BusinessException(String message, ErrorCode errorCode) {
        super(prefix + message);
        this.error = prefix + message;
        this.errorCode = errorCode;
    }

    public BusinessException(String message, Throwable throwable) {
        super(prefix + message, throwable);
        if (throwable instanceof BusinessException) {
            this.errorCode = ((BusinessException) throwable).getErrorCode();
        } else {
            this.errorCode = ErrorCodes.Common.ERROR_UNKNOWN_ERROR.errorCode();
        }
    }

    public BusinessException(ErrorCode errorCode, Throwable throwable) {
        this(errorCode.getMessage(), errorCode, throwable);
    }

    public BusinessException(String message, ErrorCode errorCode, Throwable throwable) {
        super(prefix + message, throwable);
        this.errorCode = errorCode;
    }

    public ErrorCode getErrorCode() {
        return errorCode;
    }

    public String getError() {
        return error;
    }

}
